from sklearn import svm
from sklearn.metrics import precision_score, recall_score, f1_score
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import pandas as pd

if __name__ == '__main__':
    data = pd.read_csv("./data/KaggleCredit.csv", index_col=0, nrows=15000)
    data.dropna(inplace=True)

    '''
    1.数据标准化
    '''
    cols = data.columns[1:]
    ss = StandardScaler()
    data[cols] = ss.fit_transform(data[cols])

    '''
    2.构建训练数据
    '''
    X = data.drop('SeriousDlqin2yrs', axis=1)
    y = data['SeriousDlqin2yrs']

    '''
    3.训练、测试数据划分
    '''
    X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

    '''
    4.训练
    '''
    svm_model = svm.SVC()
    # //[['NumberOfTime30-59DaysPastDueNotWorse']]
    svm_model.fit(X_train, y_train)
    print('svm_model:\n', svm_model)

    '''
     5.预测、准确率
     '''
    y_predict_svm = svm_model.predict(X_test)
    print('predict result:', y_predict_svm)

    svm_score = svm_model.score(X_test, y_predict_svm)

    test_precision_score_v = precision_score(y_test, y_predict_svm, average='macro')
    test_recall_score_v = recall_score(y_test, y_predict_svm, average='macro')
    test_f1_score_v = f1_score(y_test, y_predict_svm, average='macro')

    # train_precision_score_v = precision_score(X_train, y_train, average='macro')
    # train_recall_score_v = recall_score(X_train, y_train, average='macro')
    # train_f1_score_v = f1_score(X_train, y_train, average='macro')

    print('svm_score:', svm_score,
          " | test_precision_score_v:", test_precision_score_v,
          " | test_recall_score_v: ", test_recall_score_v)
